﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;

public class FoodView : MonoBehaviour, IPointerClickHandler
{
    public Image FoodImage;
    private Text damageText;
    private Text energyText;
    private Food food;
    string tag = "";
    private GameObject turnGame;
    private bool isShow = false;

    void Awake()
    {
        FoodImage = this.transform.GetComponent<Image>();
        damageText = transform.Find("damage").GetComponent<Text>();
        damageText.gameObject.SetActive(false);
        energyText = transform.Find("energy").GetComponent<Text>();
        energyText.gameObject.SetActive(false);
        turnGame = GameObject.FindGameObjectWithTag("TurnGame");
    }

    //想点击后才显示攻击范围
    public void OnPointerClick(PointerEventData eventData)
    {
        MapManager._instance.ClearMapGridState();
        if (isShow == false)
        {
            //Debug.Log(isShow);
            //isShow = true;
            //Debug.Log(this.gameObject.tag);
            turnGame.gameObject.SetActive(false);

            if (this.gameObject.tag == "EnemyFood")
            {
                FoodInfo foodInfo = ReadXml._instance.GetFoodBySpriteName(FoodImage.sprite.name);

                food = TeamManager._instance.GetEnemyFoodById(foodInfo.ID);
                FoodInfoView._instance.gameObject.SetActive(true);
                FoodInfoView._instance.ShowView(food);
                //Debug.Log(food.foodInfo.FoodName);

                MapManager._instance.ShowRange(food);
            }
            else if (this.gameObject.tag == "PlayerFood")
            {
                FoodInfo foodInfo = ReadXml._instance.GetFoodBySpriteName(FoodImage.sprite.name);

                food = TeamManager._instance.GetPlayerFoodById(foodInfo.ID);
                FoodInfoView._instance.gameObject.SetActive(true);
                FoodInfoView._instance.ShowView(food);

                MapManager._instance.ShowRange(food);
            }
            else
                return;

        }
        else
        {
            //Debug.Log(isShow);
            isShow = false;
            turnGame.gameObject.SetActive(true);

            MapManager._instance.HideRange(food);
            FoodInfoView._instance.gameObject.SetActive(false);

            //Debug.Log("___________________________________Exit");
        }
    }

    /// <summary>
    /// 设置图片显示的位置
    /// </summary>
    /// <param name="mapGrid"></param>
    public void SetViewPos(GameObject mapGrid)
    {
        transform.SetParent(mapGrid.transform);
        FoodImage.transform.localPosition = Vector3.zero;
        FoodImage.transform.localScale = new Vector3(1, 1, 1);
    }

    /// <summary>
    /// 显示能量
    /// </summary>
    /// <param name="e"></param>
    public void SetEnergyView(int e)
    {
        energyText.text = e.ToString();
        energyText.color = Color.green;
        energyText.gameObject.SetActive(true);

        if (e <= 0)
            energyText.gameObject.SetActive(false);
    }

    /// <summary>
    /// 显示伤害数值
    /// </summary>
    /// <param name="damage"></param>
    public void ShowDamage(string damage, Color color)
    {
        damageText.text = damage;
        damageText.fontSize = 75;
        damageText.color = color;
        //damageText.transform.SetAsLastSibling();
        damageText.gameObject.SetActive(true);
        StartCoroutine("damageWait");

    }

    IEnumerator damageWait()
    {
        yield return new WaitForSeconds(1.5f);
        damageText.gameObject.SetActive(false);
    }

    //public void OnPointerEnter(PointerEventData Data)
    //{
    //    //Debug.Log(this.gameObject.tag);
    //    turnGame.gameObject.SetActive(false);

    //    if (this.gameObject.tag == "EnemyFood")
    //    {
    //        FoodInfo foodInfo = ReadXml._instance.GetFoodBySpriteName(FoodImage.sprite.name);

    //        food = TeamManager._instance.GetEnemyFoodById(foodInfo.ID);
    //        FoodInfoView._instance.gameObject.SetActive(true);
    //        FoodInfoView._instance.ShowView(food);
    //        //Debug.Log(food.foodInfo.FoodName);

    //        MapManager._instance.ShowRange(food);
    //    }
    //    else if (this.gameObject.tag == "PlayerFood")
    //    {
    //        FoodInfo foodInfo = ReadXml._instance.GetFoodBySpriteName(FoodImage.sprite.name);

    //        food = TeamManager._instance.GetPlayerFoodById(foodInfo.ID);
    //        FoodInfoView._instance.gameObject.SetActive(true);
    //        FoodInfoView._instance.ShowView(food);

    //        MapManager._instance.ShowRange(food);
    //    }
    //    else
    //        return;

    //}

    //public void OnPointerExit(PointerEventData Data)
    //{
    //    turnGame.gameObject.SetActive(true);

    //    MapManager._instance.HideRange(food);
    //    FoodInfoView._instance.gameObject.SetActive(false);

    //    //Debug.Log("___________________________________Exit");
    //}

    public void PlayerAttackEffect(ArrayList red, string tag)
    {
        this.tag = tag;
        for (int i = 0; i < red.Count; i++)
        {
            GameObject mapgrid = null;
            //Debug.Log(red[i]);
            mapgrid = MapManager._instance.GetMapGridGo((int)red[i]);
            MapGridView mgv = mapgrid.GetComponent<MapGridView>();
            mgv.LastGridState = mgv.currenGridState;
            mapgrid.GetComponent<MapGridView>().SetState(MapGridState.red);
        }
        StartCoroutine("TurnRedBack", red);
    }

    IEnumerator TurnRedBack(ArrayList red)
    {
        yield return new WaitForSeconds(1f);
        for (int i = 0; i < red.Count; i++)
        {
            GameObject mapgrid = null;
            mapgrid = MapManager._instance.GetMapGridGo((int)red[i]);
            MapGridView mgv = mapgrid.GetComponent<MapGridView>();
            mapgrid.GetComponent<MapGridView>().SetState(mgv.LastGridState);
        }
        if (tag == "PlayerFood")
        {
            PlayerAttack._instance.PlayDamage();
        }
        else if (tag == "EnemyFood")
        {
            EnemyAI._instance.PlayDamage();
        }
        else if (tag == "PlayerHelp")
        {
            PlayerAttack._instance.HelpDamage();
        }
        else
        {
            EnemyAI._instance.HelpDamage();
        }
        tag = "";
    }

    public void HealthEffect(ArrayList green, string tag)
    {
        this.tag = tag;
        for (int i = 0; i < green.Count; i++)
        {
            GameObject mapgrid = null;
            //Debug.Log(red[i]);
            mapgrid = MapManager._instance.GetMapGridGo((int)green[i]);
            MapGridView mgv = mapgrid.GetComponent<MapGridView>();
            mgv.LastGridState = mgv.currenGridState;
            mapgrid.GetComponent<MapGridView>().SetState(MapGridState.green);
        }
        StartCoroutine("TurnGreenBack", green);
    }

    IEnumerator TurnGreenBack(ArrayList green)
    {
        yield return new WaitForSeconds(1f);
        for (int i = 0; i < green.Count; i++)
        {
            GameObject mapgrid = null;
            mapgrid = MapManager._instance.GetMapGridGo((int)green[i]);
            MapGridView mgv = mapgrid.GetComponent<MapGridView>();
            mapgrid.GetComponent<MapGridView>().SetState(mgv.LastGridState);
        }
        if (tag == "PlayerFood")
        {
            PlayerAttack._instance.AddHealth();
        }
        else if (tag == "EnemyFood")
        {
            EnemyAI._instance.AddHealth();
        }
        else if (tag == "PlayerHelp")
        {
            PlayerAttack._instance.HelpHealth();
        }
        else
        {
            EnemyAI._instance.HelpHealth();
        }
        tag = "";
    }

}
